'use strict';
const {
	wxConfigMp
} = require('config')

const uniAccount = require('uni-account')

exports.main = async (event, context) => {
	// console.log('参数', event, context)
	const {
		code
	} = event
	let uniAccountInstance
	switch (context.PLATFORM) {
		case 'mp-weixin':
			uniAccountInstance = uniAccount.initWeixin(wxConfigMp)
			break;
		default:
			return {
				code: -1,
				msg: '错误的客户端类型',
				platform: context.PLATFORM
			}
	}
	let openid
	try {
		let session = await uniAccountInstance.code2Session(code)
		console.log(session)
		openid = session.openid
	} catch (e) {
		console.log(e.message)
		return {
			code: -1,
			msg: '登录失败',
			platform: context.PLATFORM
		}
	}
	return {
		code: 0,
		data: {
			openid,
			platform: context.PLATFORM
		}
	}
};
